package com.example.pocket_demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.pocket_demo.entity.Pocket;
import com.example.pocket_demo.entity.Record;
import com.example.pocket_demo.entity.vo.RecordVO;
import com.example.pocket_demo.mapper.PocketMapper;
import com.example.pocket_demo.mapper.RecordMapper;
import com.example.pocket_demo.service.PocketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author 52656
 * @description 针对表【pocket】的数据库操作Service实现
 * @createDate 2023-04-13 17:02:49
 */
@Service
public class PocketServiceImpl extends ServiceImpl<PocketMapper, Pocket>
        implements PocketService {

    @Autowired
    PocketMapper pocketMapper;
    @Autowired
    RecordMapper recordMapper;

    @Override
    public BigDecimal getBalance(Integer userId) {
        Pocket pocket;
        Assert.notNull(userId,"param is null");
        pocket = pocketMapper.selectOne(new QueryWrapper<Pocket>()
                .eq("userId", userId));
        return pocket==null?null:pocket.getBalance();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public BigDecimal consume(Integer userId,BigDecimal amount) throws Exception {
        Assert.notNull(userId,"param is null");
        Assert.notNull(amount,"param is null");
        int res = amount.compareTo(new BigDecimal(0));
        if(res<=0){
            throw new Exception("consume amount invalid");
        }
        Pocket pocket = pocketMapper.selectOne(new QueryWrapper<Pocket>()
                .eq("userId", userId));
        if (pocket==null){
            throw new Exception("user not exist");
        }
        BigDecimal balance = pocket.getBalance();
        if(balance.compareTo(amount)<0){
            throw new Exception("balance is not enough");
        }
        pocket.setBalance(balance.subtract(amount));
        pocketMapper.updateById(pocket);
        Record record = new Record();
        record.setAmount(amount);
        record.setType(0);
        record.setPocketid(pocket.getPocketid());
        record.setUpdatetime(new Date());
        recordMapper.insert(record);
        return pocket.getBalance();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public BigDecimal refund(Integer userId, BigDecimal amount) throws Exception {
        Assert.notNull(userId,"param is null");
        Assert.notNull(amount,"param is null");
        Pocket pocket = pocketMapper.selectOne(new QueryWrapper<Pocket>()
                .eq("userId", userId));
        if (pocket==null){
            throw new Exception("user not exist");
        }
        BigDecimal balance = pocket.getBalance();
        if(balance.compareTo(amount)<0){
            throw new Exception("balance is not enough");
        }
        pocket.setBalance(balance.add(amount));
        pocketMapper.updateById(pocket);
        Record record = new Record();
        record.setAmount(amount);
        record.setType(1);
        record.setPocketid(pocket.getPocketid());
        record.setUpdatetime(new Date());
        recordMapper.insert(record);
        return pocket.getBalance();
    }

    @Override
    public List<RecordVO> listIncomeRefund(Integer pocketid, Integer page, Integer pageSize) {
        if(page<=0)
            return null;
        List<RecordVO> recordList = recordMapper.listIncomeRefund(pocketid,pageSize,pageSize*(page-1));
        return recordList;
    }


}




